package org.example.cabinetmachine.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.example.cabinetmachine.service.CabinetMachineService;
import org.example.config.gaud.AmapGeocoder;
import org.example.config.gaud.ReverseGeocodingTest;
import org.example.model.common.Result;
import org.example.model.common.ResultCodeEnum;
import org.example.model.domain.CabinetMachine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cabinetmachine")
@Slf4j
public class CabinetMachineController {

    @Autowired
    CabinetMachineService cabinetMachineService;

    @GetMapping("/list")
    public Result<CabinetMachine> list(CabinetMachine cabinetMachine) {
        List<CabinetMachine> cabinetMachine1 = cabinetMachineService.findCabinetMachine(cabinetMachine);
        return Result.build(cabinetMachine1, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/updatechecked/{id}")
    public Result<CabinetMachine> updatechecked(@PathVariable int id) {
        int i = cabinetMachineService.updatechecked(id);
        return Result.build(i, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/updatecheckedzero/{id}")
    public Result<CabinetMachine> updatecheckedzero(@PathVariable int id) {
        int i = cabinetMachineService.updatecheckedzero(id);
        return Result.build(i, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/updateisCollection/{ids}")
    public Result<CabinetMachine> updateisCollection(@PathVariable Integer ids) {
        System.out.println(ids);
        int i = cabinetMachineService.updateisCollection(ids);
        return Result.build(i, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/Bookmarked/{id}")
    public Result<CabinetMachine> Bookmarked(@PathVariable int id) {
        int bookmarked = cabinetMachineService.Bookmarked(id);
        return Result.build(bookmarked, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/Unfavorites/{id}")
    public Result<CabinetMachine> Unfavorites(@PathVariable int id) {
        int unfavorites = cabinetMachineService.Unfavorites(id);
        return Result.build(unfavorites, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据机柜编号获取地址信息
     *
     * @param cabinetMachine 包含机柜编号的对象，用于查询机柜位置
     * @return 返回一个Result对象，包含查询结果或错误信息
     */
    @GetMapping("getaddress")
    public Result getaddress(CabinetMachine cabinetMachine) {
        // 根据机柜编号查询对应的机柜机器信息
        List<CabinetMachine> cabinetMachine1 = cabinetMachineService.findCabinetMachine(cabinetMachine);
        String address = null;
        // 如果查询结果非空且不为空列表
        if (cabinetMachine1 != null && !cabinetMachine1.isEmpty()) {
            address = cabinetMachine1.get(0).getLocation();
        }
        // 打印地址信息
        log.info("地址: {}", address);
        // 根据地址信息获取经纬度
        Map<String, String> geocoderLatitude = AmapGeocoder.getGeocoder(address);
        // 如果未获取到经纬度信息
        if (geocoderLatitude == null) {
            // 返回失败结果
            return Result.build(null, ResultCodeEnum.FAIL);
        }
        // 打印经纬度信息
        log.info("经纬度: {}", geocoderLatitude);
        // 返回成功结果，包含经纬度信息
        return Result.build(geocoderLatitude, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("ReverseGeocoding")
    public Result ReverseGeocoding(double longitude, double latitude) throws Exception {
        String s = ReverseGeocodingTest.reverseGeocode(longitude, latitude);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(s);
        // 获取格式化后的地址
        String formattedAddress = rootNode.get("regeocode").get("formatted_address").asText();
        log.info("格式化后的地址：{}", formattedAddress);
        // 获取省份
        String province = rootNode.get("regeocode").get("addressComponent").get("province").asText();
        log.info("省份：{}", province);
        // 获取城市
        String city = rootNode.get("regeocode").get("addressComponent").get("city").asText();
        log.info("城市：{}", city);
        // 获取区
        String district = rootNode.get("regeocode").get("addressComponent").get("district").asText();
        log.info("区：{}", district);
        // 获取乡镇
        String township = rootNode.get("regeocode").get("addressComponent").get("township").asText();
        log.info("乡镇：{}", township);
        // 获取门牌号
        String number = rootNode.get("regeocode").get("addressComponent").get("streetNumber").get("number").asText();
        log.info("门牌号：{}", number);
        return Result.build(formattedAddress, ResultCodeEnum.SUCCESS);
    }


}

